home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / src / win.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  7.1 KB  |  279 lines

  1. /* Curses utilities
  2.    Copyright (C) 1995 Miguel de Icaza, Janne Kukonlehto
  3.    
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2 of the License, or
  7.    (at your option) any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <config.h>
  19. #include "tty.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>    /* For free() */
  22. #include <string.h>
  23. #include <termios.h>
  24. #include "mad.h"
  25. #include "color.h"
  26. #include "mouse.h"
  27. #include "util.h"    /* For xmalloc() */
  28.  
  29. #include "dlg.h"
  30. #include "widget.h"
  31. #include "win.h"
  32. #include "key.h"        /* XCTRL and ALT macros  */
  33. #include "layout.h"
  34. #include "global.h"
  35.  
  36. /* "$Id: win.c,v 1.10 1995/02/21 19:07:47 miguel Exp $" */
  37.  
  38. typedef void (*fnptr)(void);
  39.  
  40. typedef struct Fkey_Table_List {
  41.     fnptr  actions[11];
  42.     struct Fkey_Table_List *next;
  43.     int    has_labels;
  44. } Fkey_Table_List;
  45.  
  46. static Fkey_Table_List *fkey_table_list = NULL;
  47.  
  48. /* Width of output is always seven characters */
  49. void sprint_bytesize (char *buffer, int size, int scale)
  50. {
  51.     char scales[] = " kMGT";
  52.  
  53.     if (size > 0){
  54.     while (size > 9999 && scale < sizeof (scales)){
  55.         size = (size + 512) / 1024;
  56.         scale ++;
  57.     }
  58.     }
  59.     if (scale > 0)
  60.     sprintf (buffer, "%4d %cb", size, scales[scale]);
  61.     else
  62.     sprintf (buffer, "%4d b ", size);
  63. }
  64.  
  65. void print_bytesize (int size, int scale)
  66. {
  67.     char buffer [10];
  68.     
  69.     sprint_bytesize (buffer, size, scale);
  70.     printw (buffer);
  71. }
  72.  
  73. /* Return values: 0 = not a fkey, other = was a fkey */
  74. int check_fkeys (int c)
  75. {
  76.     int fkey;
  77.  
  78.     if (!fkey_table_list)
  79.     return 0;
  80.     
  81.     switch (c){
  82.     case KEY_F(1):
  83.     fkey = 1;
  84.     break;
  85.     case KEY_F(2):
  86.     fkey = 2;
  87.     break;
  88.     case KEY_F(3):
  89.     fkey = 3;
  90.     break;
  91.     case KEY_F(4):
  92.     fkey = 4;
  93.     break;
  94.     case KEY_F(5):
  95.     fkey = 5;
  96.     break;
  97.     case KEY_F(6):
  98.     fkey = 6;
  99.     break;
  100.     case KEY_F(7):
  101.     fkey = 7;
  102.     break;
  103.     case KEY_F(8):
  104.     fkey = 8;
  105.     break;
  106.     case KEY_F(9):
  107.     fkey = 9;
  108.     break;
  109.     case KEY_F(10):
  110.     fkey = 10;
  111.     break;
  112.     default:
  113.     return 0;
  114.     }
  115.     if (fkey_table_list->actions [fkey]){
  116.     fkey_table_list->actions [fkey] ();
  117.     return fkey;
  118.     }
  119.     else
  120.     return 0;
  121. }
  122.  
  123. /* Return values: 0 = not a movement key, 1 = was a movement key */
  124. int check_movement_keys (int c, int additional, int page_size, void *data,
  125.              movefn backfn, movefn forfn, movefn topfn,
  126.              movefn bottomfn)
  127. {
  128.     switch (c){
  129.     case KEY_UP:
  130.     case XCTRL ('p'):
  131.     (*backfn)(data, 1);
  132.     return 1;
  133.     
  134.     case KEY_DOWN:
  135.     case XCTRL ('n'):
  136.     (*forfn)(data, 1);
  137.     return 1;
  138.     
  139.     case KEY_PPAGE:
  140.     case ALT('v'):
  141.     (*backfn)(data, page_size-1);
  142.     return 1;
  143.     
  144.     case KEY_NPAGE:
  145.     case XCTRL('v'):
  146.     (*forfn)(data, page_size-1);
  147.     return 1;
  148.     
  149.     case KEY_HOME:
  150.     case KEY_A1:
  151.     (*topfn)(data, 0);
  152.     return 1;
  153.     case KEY_END:
  154.     case KEY_C1:
  155.     (*bottomfn)(data, 0);
  156.     return 1;
  157.     }
  158.     if (additional)
  159.         switch (c){
  160.     case 'b':  
  161.     case XCTRL('h'):
  162.     case KEY_BACKSPACE:
  163.     case 0177:
  164.         (*backfn)(data, page_size-1);
  165.         return 1;
  166.     case ' ':
  167.         (*forfn)(data, page_size-1);
  168.         return 1;
  169.     case 'u':
  170.         (*backfn)(data, page_size / 2);
  171.         return 1;
  172.     case 'd':
  173.         (*forfn)(data, page_size / 2);
  174.         return 1;
  175.     case 'g':
  176.         (*topfn)(data, 0);
  177.         return 1;
  178.     case 'G':
  179.         (*bottomfn)(data, 0);
  180.         return 1;
  181.     }
  182.     return 0;
  183. }
  184.  
  185. void mc_raw_mode (void)
  186. {
  187.     raw ();
  188. }
  189.  
  190. void mc_noraw_mode (void)
  191. {
  192.     noraw ();
  193. }
  194.  
  195. /* Classification routines */
  196. int is_abort_char (int c)
  197. {
  198.     return (c == XCTRL('c') || c == XCTRL('g') || c == ESC_CHAR ||
  199.         c == KEY_F(10));
  200. }
  201.  
  202. int is_quit_char (int c)
  203. {
  204.     return (c == XCTRL('g') || (c == ESC_CHAR) || (c == KEY_F(10)));
  205. }
  206.  
  207. /* This table is a mapping between names and the constants we use
  208.  * We use this to allow users to define alternate definitions for
  209.  * certain keys that may be missing from the terminal database
  210.  */
  211. key_code_name_t key_name_conv_tab [] = {
  212. /* KEY_F(0) is not here, since we are mapping it to f10, so there is no reason
  213.    to define f0 as well. Also, it makes Learn keys a bunch of problems :( */
  214.     { KEY_F(1),      "f1", "Function key 1" },
  215.     { KEY_F(2),      "f2", "Function key 2" },
  216.     { KEY_F(3),      "f3", "Function key 3" },
  217.     { KEY_F(4),      "f4", "Function key 4" },
  218.     { KEY_F(5),      "f5", "Function key 5" },
  219.     { KEY_F(6),      "f6", "Function key 6" },
  220.     { KEY_F(7),      "f7", "Function key 7" },
  221.     { KEY_F(8),      "f8", "Function key 8" },
  222.     { KEY_F(9),      "f9", "Function key 9" },
  223.     { KEY_F(10),     "f10", "Function key 10" },
  224.     { KEY_F(11),     "f11", "Function key 11" },
  225.     { KEY_F(12),     "f12", "Function key 12" },
  226.     { KEY_F(13),     "f13", "Function key 13" },
  227.     { KEY_F(14),     "f14", "Function key 14" },
  228.     { KEY_F(15),     "f15", "Function key 15" },
  229.     { KEY_F(16),     "f16", "Function key 16" },
  230.     { KEY_F(17),     "f17", "Function key 17" },
  231.     { KEY_F(18),     "f18", "Function key 18" },
  232.     { KEY_F(19),     "f19", "Function key 19" },
  233.     { KEY_F(20),     "f20", "Function key 20" },
  234.     { KEY_BACKSPACE, "bs", "Backspace key" },
  235.     { KEY_END,       "end", "End key" },
  236.     { KEY_UP,        "up", "Up arrow key" },
  237.     { KEY_DOWN,      "down", "Down arrow key" },
  238.     { KEY_LEFT,      "left", "Left arrow key" },
  239.     { KEY_RIGHT,     "right", "Right arrow key" },
  240.     { KEY_HOME,      "home", "Home key" },
  241.     { KEY_NPAGE,     "pgdn", "Page Down key" },
  242.     { KEY_PPAGE,     "pgup", "Page Up key" },
  243.     { KEY_IC,        "insert", "Insert key" },
  244.     { KEY_DC,        "delete", "Delete key" },
  245.     { ALT('\t'),     "complete", "Completion (M-tab)" },
  246.     { KEY_KP_ADD,    "kpplus", "+ on keypad" },
  247.     { KEY_KP_SUBTRACT,"kpminus", "- on keypad" },
  248.     { KEY_KP_MULTIPLY,"kpasterix", "* on keypad" },
  249. /* From here on, these won't be shown in Learn keys (no space) */    
  250.     { KEY_LEFT,         "kpleft", "Left arrow keypad" },
  251.     { KEY_RIGHT,     "kpright", "Right arrow keypad" },
  252.     { KEY_UP,         "kpup", "Up arrow keypad" },
  253.     { KEY_DOWN,         "kpdown", "Down arrow keypad" },
  254.     { KEY_HOME,         "kphome", "Home on keypad" },
  255.     { KEY_END,         "kpend", "End on keypad" },
  256.     { KEY_NPAGE,     "kpnpage", "Page Down keypad" },
  257.     { KEY_PPAGE,     "kpppage", "Page Up keypad" },
  258.     { KEY_IC,        "kpinsert","Insert on keypad" },
  259.     { KEY_DC,        "kpdelete","Delete on keypad" },
  260.     { (int) '\n',    "kpenter", "Enter on keypad" },
  261.     { (int) '/',     "kpslash", "Slash on keypad" },
  262.     { (int) '#',     "kpnumlock", "NumLock on keypad" },
  263.     { 0, 0 }
  264.     };
  265.  
  266. /* Return the code associated with the symbolic name keyname */
  267. int lookup_key (char *keyname)
  268. {
  269.     int i;
  270.  
  271.     for (i = 0; key_name_conv_tab [i].code; i++){
  272.     if (strcasecmp (key_name_conv_tab [i].name, keyname))
  273.         continue;
  274.     return key_name_conv_tab [i].code;
  275.     }
  276.     return 0;
  277. }
  278.  
  279.